﻿using Application.Abstractions.Messaging;
using Application.Todos.Create;
using Domain.Todos;
using Mediator;
using SharedKernel;
using WebApi.Extensions;
using WebApi.Infrastructure;

namespace WebApi.Endpoints.Todos;

internal sealed class Create : IEndpoint
{
    public sealed class Request
    {
        public Guid UserId { get; set; }
        public string Description { get; set; }
        public DateTime? DueDate { get; set; }
        public List<string> Labels { get; set; } = [];
        public int Priority { get; set; }
    }

    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost("todos", async (
                Request request,
                IMediator mediator,
                CancellationToken cancellationToken) =>
            {
                var command = new CreateTodoCommand
                {
                    UserId = request.UserId,
                    Description = request.Description,
                    DueDate = request.DueDate,
                    Labels = request.Labels,
                    Priority = (Priority)request.Priority
                };

                Result<Guid> result = await mediator.Send(command, cancellationToken);
                return result.Match(Results.Ok, ApiResponse.Problem);
            })
            .WithTags(Tags.Todos)
            .HasApiVersion(1, 0);
    }
}
